﻿/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: FZH
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using FriendStar.Code;
using FriendStar.Domain.Entity.BOM;
using FriendStar.Domain.IRepository.BOM;
using FriendStar.Repository.BOM;

namespace FriendStar.Application.BOM
{
    /// <summary>
	/// 数据表 CLDE_41_YL_BLCorner 对应的数据应用功能类。
	/// </summary>
	public class CLDE_41_YL_BLCornerApp
    {       
        private string UseUnit = Configs.GetValue("UseUnit");  

        private ICLDE_41_YL_BLCornerRepository service = new CLDE_41_YL_BLCornerRepository();

        public List<CLDE_41_YL_BLCornerEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<CLDE_41_YL_BLCornerEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.FProductionNO.Contains(keyword));
            }
            return service.FindList(expression, pagination);
        }

        public CLDE_41_YL_BLCornerEntity GetForm(string keyValue)
        {
            var expression = ExtLinq.True<CLDE_41_YL_BLCornerEntity>();
            expression = expression.And(t => t.MaterialID == keyValue);
            return service.FindEntity(keyValue);
        }

        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.MaterialID == keyValue);
        }

        public void SubmitForm(CLDE_41_YL_BLCornerEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Update(entity);
            }
            else
            {
                entity.MaterialID = Common.GuId();
                entity.UseUnit = UseUnit;
                entity.FCreateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FCreateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FCreateTime = DateTime.Now.ToString("HH:mm:ss");
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Insert(entity);
            }
        }

        public bool CheckedEntity(string FProductionNO, string MaterialCode)
        {
            var expression = ExtLinq.True<CLDE_41_YL_BLCornerEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.FProductionNO == FProductionNO);
            expression = expression.And(t => t.MaterialCode == MaterialCode);
            var list = service.FindList(expression);
            return list.Count == 0;
        }

        public bool CheckedEntity(CLDE_41_YL_BLCornerEntity entity)
        {
            var expression = ExtLinq.True<CLDE_41_YL_BLCornerEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.FProductionNO == entity.FProductionNO);
            expression = expression.And(t => t.MaterialCode == entity.MaterialCode);
            if (!string.IsNullOrEmpty(entity.MaterialID))
            {
                expression = expression.And(t => t.MaterialID != entity.MaterialID);
            }
            var oldEntityList = service.FindList(expression);
            return oldEntityList.Count == 0;
        }
    }
}